package com.mongoosehome.web.common.action;

/**
 * 上传处理Action。
 */
import com.mongoosehome.web.base.BaseActon;
import com.mongoosehome.web.util.BaseResource;
import com.mongoosehome.web.util.RequestUtils;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;

public class UploaderAction extends BaseActon {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private File filedata;
    private String filedataFileName;
    private String filedataContentType;

    @Override
    public String execute() throws Exception {       
        
        String extName = ""; //文件拓展名 
        //获取拓展名 
        if (this.filedataFileName.lastIndexOf(".") >= 0) {
            extName = this.filedataFileName.substring(this.filedataFileName.lastIndexOf("."));
        }
        
        String pid = RequestUtils.getStringParameter(request, "pid", "");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String fileNameTemp = sdf.format(new Date());
        fileNameTemp = fileNameTemp+"_"+pid;

        String rootPath = ServletActionContext.getServletContext().getRealPath("/");
        String filePath = BaseResource.getPathValue("product.upload.path.temp",File.separator,false,true);
        /*
        if (filePath.startsWith(File.separator)) {
            filePath = filePath.substring(1);
        }
        if (!(filePath.endsWith(File.separator))) {
            filePath += File.separator;
        }*/

        String saveFileName = rootPath + filePath + fileNameTemp + extName;
        File dest = new File( rootPath + filePath);
        if (!dest.exists()) {
            dest.mkdirs();
        }
        FileUtils.copyFile(filedata, new File(saveFileName));
        //filedata.renameTo(new File(saveFileName));
        JSONObject obj = new JSONObject();
        obj.put("statusCode", 200);
        obj.put("message", "文件(" + this.filedataFileName + ")上传成功!");
        obj.put("saveFileName", fileNameTemp + extName);
        this.responseWriteJson(obj.toString());
        return null;
    }

    /**
     * @return the filedata
     */
    public File getFiledata() {
        return filedata;
    }

    /**
     * @param filedata the filedata to set
     */
    public void setFiledata(File filedata) {
        this.filedata = filedata;
    }

    /**
     * @return the filedataFileName
     */
    public String getFiledataFileName() {
        return filedataFileName;
    }

    /**
     * @param filedataFileName the filedataFileName to set
     */
    public void setFiledataFileName(String filedataFileName) {
        this.filedataFileName = filedataFileName;
    }

    /**
     * @return the filedataContentType
     */
    public String getFiledataContentType() {
        return filedataContentType;
    }

    /**
     * @param filedataContentType the filedataContentType to set
     */
    public void setFiledataContentType(String filedataContentType) {
        this.filedataContentType = filedataContentType;
    }


}
